package org.gradle;

import javax.websocket.Decoder;
import javax.websocket.Encoder;
import javax.websocket.Endpoint;
import javax.websocket.server.ServerApplicationConfig;
import javax.websocket.server.ServerEndpointConfig;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class ProgrammaticServerConfig implements ServerApplicationConfig {

   /* (non-Javadoc)
     * @see javax.websocket.server.ScanServerApplicationConfig#getAnnotatedEndpointClasses(java.util.Set)
     */
    /**
     * 此方法在部署应用时被WebSocket实现调用。其传入的参数scanned是一个集合，它包含了所有通过@ServerEndpoint注解的Java类
     */
    @Override
    public Set<Class<?>> getAnnotatedEndpointClasses(Set<Class<?>> scanned) {
        return scanned;
    }

    /* (non-Javadoc)
     * @see javax.websocket.server.ScanServerApplicationConfig#getEndpointConfigs(java.util.Set)
     */
    /**
     * 此方法在部署应用时被WebSocket实现调用。其传入的参数endpointClasses是一个集合，它包含了所有继承了Endpoint类的Java类
     */
    @Override
    public Set<ServerEndpointConfig> getEndpointConfigs(
            Set<Class<? extends Endpoint>> endpointClasses) {
        Set<ServerEndpointConfig> configs = new HashSet<ServerEndpointConfig>();
        List<Class<? extends Decoder>> decoders = new ArrayList<>();
        decoders.add(WebSocketDncoders.class);
        List<Class<? extends Encoder>> encoders = new ArrayList<>();
        encoders.add(WebSocketEncoders.class);
        ServerEndpointConfig sec = ServerEndpointConfig.Builder
                .create(EncoServerProgram.class, "/programmaticServer").encoders(encoders).decoders(decoders).build();
        configs.add(sec);
        return configs;
    }
}
